<?php
	
	header("Content-Type: text/html;charset=utf-8");
	session_start(); //开启session

	$uid = $_SESSION["uid"];

	$servername = "localhost";
	$username = "root";
	$password = "456789";
	$dbname = "mysqlstudy";

	$conn = new mysqli($servername, $username, $password, $dbname);
	if($conn -> connect_error){
		die("连接失败:" . $conn -> connect_error);
	}
	mysqli_query($conn, "set names utf8");

	$roleidSql = "SELECT roleid FROM use_role WHERE userid='{$uid}'";
	$roleidResult = $conn->query($roleidSql);
	$roleidArr = $roleidResult->fetch_all(MYSQLI_NUM);  //用户对应的角色
	
	$funArr=array();   //角色对应的功能
	$funArrEd=array(); //处理过后的功能数组，去重
	$funame=array();

	for($i=0;$i<count($roleidArr);$i++){
		$functidSql = "SELECT functid FROM role_fun WHERE roleid='{$roleidArr[$i][0]}'";
		$functidResult = $conn->query($functidSql);
		$functidArr = $functidResult->fetch_all(MYSQLI_NUM);
		array_push($funArr, $functidArr);
	}

	for($i=0;$i<count($funArr);$i++){
		for($j=0;$j<count($funArr[$i]);$j++){
			array_push($funArrEd, $funArr[$i][$j][0]);
		}
	}

	$funArrEd = array_unique($funArrEd);  //去重，最终得到功能id

	for($i=0;$i<count($funArrEd);$i++){
		$funameSql = "SELECT name FROM function WHERE code='{$funArrEd[$i]}'";
		$funameResult = $conn->query($funameSql);
		$funameArr = $funameResult->fetch_array(MYSQLI_ASSOC);
		array_push($funame, $funameArr['name']);
	}
	
	echo json_encode($funame);

?>